package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.DTO.BarcodeMachineDownloadDTO;
import com.source.lightfowl.DTO.BarcodeMachineUploadDTO;
import com.source.lightfowl.VO.BarcodeMachineVO;
import com.source.lightfowl.basedata.dao.BarcodeMachineDao;
import com.source.lightfowl.basedata.dao.SlaughterHouseDao;
import com.source.lightfowl.basedata.dao.StallsDao;
import com.source.lightfowl.basedata.entity.BarcodeMachine;
import com.source.lightfowl.basedata.entity.BarcodeMachineMsg;
import com.source.lightfowl.basedata.entity.SlaughterHouse;
import com.source.lightfowl.basedata.entity.Stalls;
import com.source.lightfowl.basedata.service.BarcodeMachineMsgService;
import com.source.lightfowl.basedata.service.BarcodeMachineService;
import com.source.lightfowl.basedata.service.SlaughterHouseService;
import com.source.lightfowl.handler.MyException;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.person.entity.UsersExtends;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 12:04
 */
@Service
public class BarcodeMachineServiceImpl implements BarcodeMachineService {

    @Autowired
    private BarcodeMachineDao barcodeMachineDao;
    @Autowired
    private PersonDao personDao;
    @Autowired
    private SlaughterHouseService slaughterHouseService;
    @Autowired
    private StallsDao stallsDao;
    @Autowired
    private SlaughterHouseDao slaughterHouseDao;
    @Autowired
    private BarcodeMachineMsgService barcodeMachineMsgService;

    @Override
    public List<BarcodeMachineVO> findAll() {
        List<BarcodeMachine> list = barcodeMachineDao.findAll();
        List<BarcodeMachineVO> collect = list.stream().map(s -> {
            BarcodeMachineVO vo = new BarcodeMachineVO();
            BeanUtils.copyProperties(s, vo);

            //Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getUserId()).longValue()));
            Person user = personDao.findVO(s.getUserId());
            vo.setUserName(user.getLoginName());

            Optional<SlaughterHouse> optional1 = slaughterHouseService.findById(Integer.valueOf(s.getSlaughterHouseId()));
            if (optional1.isPresent()) {
                vo.setHouseName(optional1.get().getName());
            }

            //档口名赋值
            String stallsId = s.getStallsId();
            Integer integer = Integer.valueOf(stallsId);
            Optional<Stalls> optional = stallsDao.findById(integer);
            if (optional.isPresent()) {
                vo.setStallsName(optional.get().getStallsName());
            }

            return vo;
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public Page<BarcodeMachine> findAllPage(int page, int pageNum) {
        return barcodeMachineDao.findAll(PageRequest.of(page - 1, pageNum));
    }

    @Override
    public BarcodeMachineVO findById(String id) {
        Optional<BarcodeMachine> optional = barcodeMachineDao.findById(id);
        if (optional.isPresent()) {
            BarcodeMachine s = optional.get();
            BarcodeMachineVO vo = new BarcodeMachineVO();
            BeanUtils.copyProperties(s, vo);

            Person user = personDao.findVO(s.getUserId());
            vo.setUserName(user.getLoginName());

            Optional<SlaughterHouse> optional1 = slaughterHouseService.findById(Integer.valueOf(s.getSlaughterHouseId()));
            if (optional1.isPresent()) {
                vo.setHouseName(optional1.get().getName());
            }


            //档口名赋值
            String stallsId = s.getStallsId();
            Integer integer = Integer.valueOf(stallsId);
            Optional<Stalls> optional2 = stallsDao.findById(integer);
            if (optional2.isPresent()) {
                vo.setStallsName(optional2.get().getStallsName());
            }


            return vo;
        }
        return new BarcodeMachineVO();
    }

    @Override
    public void save(BarcodeMachine barcodeMachine) {
        //设置创建时间
        if (barcodeMachine.getCreateDate() == null) {
            barcodeMachine.setCreateDate(new Date());
        }
        barcodeMachineDao.save(barcodeMachine);
    }

    @Override
    public void deleteById(String id) {
        //barcodeMachineDao.deleteById(id);
        barcodeMachineMsgService.cascadingDeleteById(id);//级联删除
    }

    @Override
    public void deleteBatch(String ids) {

        //ArrayList<Integer> list = StringTOArrayList.toArrayList(ids);

        String[] strings = ids.split(",");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }
        //return list;

        if (list != null) {
            barcodeMachineDao.deleteBatch(list);
        } else {
            System.out.println("==========传递参数出错=============");
        }

    }

    @Override
    public Long getCount() {
        return barcodeMachineDao.count();
    }

    @Override
    public void save(List<Object> list, HttpServletRequest request) {
//        UsersExtends user = (UsersExtends) request.getSession().getAttribute("usersExtends");
//        String loginName = user.getUser().getLoginName();
        List<BarcodeMachine> machineList = new ArrayList<>();
        for (Object object :
                list) {
            BarcodeMachineUploadDTO uploadDTO = (BarcodeMachineUploadDTO) object;
            BarcodeMachine barcodeMachine = new BarcodeMachine();
            if (uploadDTO == null){
                throw new MyException(500,"此行数据为空！");
            }else {
                BeanUtils.copyProperties(uploadDTO,barcodeMachine);
                barcodeMachine.setSlaughterHouseId(slaughterHouseDao.findIdByName(uploadDTO.getSlaughterHouseName()).toString());
                barcodeMachine.setUserId(personDao.findByName(uploadDTO.getUserName()).getId());
                barcodeMachine.setStallsId(String.valueOf(stallsDao.findInfoByName(uploadDTO.getStallsName()).getId()));
                barcodeMachine.setCreateBy(" ");
                barcodeMachine.setCreateDate(new Date());
                machineList.add(barcodeMachine);
            }

        }
        barcodeMachineDao.saveAll(machineList);
    }

    @Override
    public List<Object> findAllDTO() {
        List<BarcodeMachine> barcodeMachines = barcodeMachineDao.findAll();
        if (barcodeMachines == null){
            throw new MyException(500,"数据库为空！");
        }
        List<Object> list = new ArrayList<>();
        for (BarcodeMachine barcodeMachine :
                barcodeMachines) {
            BarcodeMachineDownloadDTO downloadDTO = new BarcodeMachineDownloadDTO();
            BeanUtils.copyProperties(barcodeMachine,downloadDTO);
            downloadDTO.setSlaughterHouseName(slaughterHouseDao.findNameById(Integer.parseInt(barcodeMachine.getSlaughterHouseId())));
            downloadDTO.setUserName(personDao.findVO(barcodeMachine.getUserId()).getName());
            downloadDTO.setStallsName(stallsDao.findInfoById(barcodeMachine.getStallsId()).getStallsName());
            list.add(downloadDTO);
        }
        return list;
    }
}
